#include "KmyWorld.h"

KmyWorld::KmyWorld(){
    parent = NULL;
}

KmyWorld::KmyWorld(int W, int H)
{
    world_W = W;
    world_H = H;

    parent = NULL;

    camera.x=0;
    camera.y=0;
    camera.w=world_W;
    camera.h=world_H;
}

KmyWorld::~KmyWorld(){

}

void KmyWorld::initalize(int w, int h){
    world_W = w;
    world_H = h;
}

void KmyWorld::finalize(){
    ST_VectorNode *p = child_list.getHead();
    while (p!=NULL){
        KmyNode* node = (KmyNode*)(p->value);
        node->finalize();
        delete node;
        p = p->next;
    }
    child_list.clear();
    if (parent!=NULL){
        parent->removeChild(this);
    }
}

void KmyWorld::setCameraRange(int w, int h){
    camera.w = w;
    camera.h = h;
}

void KmyWorld::setCameraCenter(int x, int y){
    s32 cx = x-(camera.w>>1);
    s32 cy = y-(camera.h>>1);

    if (cx<0){
        cx = 0;
    }else if(cx>(world_W-camera.w)){
        cx = world_W-camera.w;
    }
    if (cy<0){
        cy = 0;
    }else if(cy>(world_H-camera.h)){
        cy = world_H-camera.h;
    }

    camera.x = cx;
    camera.y = cy;
}

St_Rect* KmyWorld::getCamera(){
    return &camera;
}

//void KmyWorld::setMap(int id, KmyMap *map){
//    maps[id] = map;
//    // 计算map的移动比率
//    if (world_W-camera.w<=0){
//        map->move_ratio_x = 0;
//    }else if (map->getMapWidth()-camera.w<=0){
//        map->move_ratio_x = 0;
//    }else{
//        map->move_ratio_x = (map->getMapWidth()-camera.w)/(world_W-camera.w);
//    }
//    if (world_H-camera.h<=0){
//        map->move_ratio_y = 0;
//    }else if (map->getMapHeight()-camera.h<=0){
//        map->move_ratio_y = 0;
//    }else{
//        map->move_ratio_y = (map->getMapHeight()-camera.h)/(world_H-camera.h);
//    }
//}

//KmyMap* KmyWorld::getMap(int id){
//    return maps[id];
//}

void KmyWorld::logic(){
//    for (int i=0; i<map_len; i++){
//        if (maps[i]!=NULL){
//            maps[i]->logic(&camera);
//        }
//    }
    ST_VectorNode *p = child_list.getHead();
    while(p!=NULL){
        KmyMap* map = (KmyMap*)p->value;
        map->logic(&camera);
        p = p->next;
    }
}

void KmyWorld::render(KmyICanvas* screen){
//    for (int i=0; i<map_len; i++){
//        if (maps[i]!=NULL){
//            maps[i]->render(&camera, screen);
//        }
//    }
    ST_VectorNode *p = child_list.getHead();
    while(p!=NULL){
        KmyNode* node = (KmyNode*)p->value;
        node->render(screen);
        p = p->next;
    }
}

void KmyWorld::addMap(KmyMap *map){
    if (map->parent!=NULL){
        return;
    }
    if (map->id == 0){
        map->id = KmyNode::createID();
    }
    child_list.addValue((void*)map);
    map->parent = this;
    if (this->world_W<=camera.w){
        map->move_ratio_x = 1;
    }else{
        map->move_ratio_x = (map->getMapWidth()-camera.w)/(this->world_W-camera.w);
    }
    if (this->world_H<=camera.h){
        map->move_ratio_y = 1;
    }else{
        map->move_ratio_y = (map->getMapHeight()*camera.h)/(this->world_H-camera.h);
    }

}

void KmyWorld::removeMap(KmyMap *map){
    if (map->parent!=this){
        return;
    }
    child_list.removeValue((void*)map);
    map->parent = NULL;
}

KmyMap* KmyWorld::getMap(int index){
    void* p = child_list.getValueByIndex(index);
    if (p!=NULL){
        return (KmyMap*)p;
    }
    return NULL;
}

int KmyWorld::getGlobalX(){
    return 0;
}

int KmyWorld::getGlobalY(){
    return 0;
}
